/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.graphs3d;

import data.CompressionGraphData;
import data.CompressionGraphObject;
import java.text.DecimalFormat;
import java.util.Vector;
import javax.media.j3d.Transform3D;
import javax.vecmath.Vector3f;

import javax.media.j3d.Transform3D;
import javax.vecmath.Vector3f;

import main.ToEngineFacade;

/**
 *
 * @author Elad
 */
public class ThinProvisioningGraph extends Graph3D {

    private CompressionGraphData gData;

    public ThinProvisioningGraph(CompressionGraphData gData) {
        super();
        this.gData = gData;
        if (j3dInstalled) {
            addGraphPoints();
            addAxises(new Positive2DAxises());
            setOrbitBehavior();
            add3DText("Empty Disk Amount", 1.11, 0.0, 0.0, 0.05, 0.05, 0.05);
            add3DText("Performance", -0.17, 1.02, 0.0, 0.05, 0.05, 0.05);
            finishGraphCreation();
        }
    }

    @Override
    protected void addGraphPoints() {
        //      System.out.println("hey " + gData.getClass());
        Vector<CompressionGraphObject> allPoints = gData.get_amountResults();
        Vector<PointValues> normalizedPoints = normalize(allPoints);
        Vector<PointValues> unitedPoints = unitepoints(normalizedPoints);
        addTextMarks(allPoints);
        PointValues p;
        for (int i = 0; i < unitedPoints.size(); i++) {
            p = unitedPoints.get(i);
            addPoint(p.getX(), p.getY(), p.getZ(), p.getMsg());
        }
    }

    private void addTextMarks(Vector<CompressionGraphObject> allPoints) {
        double minPer = allPoints.get(0).get_performance();
        double maxPer = allPoints.get(0).get_performance();
        double minDiskAmount = allPoints.get(0).get_emptyDiskAmount();
        double maxDiskAmount = allPoints.get(0).get_emptyDiskAmount();
        for (int i = 1; i < allPoints.size(); i++) {
            if (minPer > allPoints.get(i).get_performance()) {
                minPer = allPoints.get(i).get_performance();
            }
            if (maxPer < allPoints.get(i).get_performance()) {
                maxPer = allPoints.get(i).get_performance();
            }
            if (maxDiskAmount < allPoints.get(i).get_emptyDiskAmount()) {
                maxDiskAmount = allPoints.get(i).get_emptyDiskAmount();
            }
            if (minDiskAmount > allPoints.get(i).get_emptyDiskAmount()) {
                minDiskAmount = allPoints.get(i).get_emptyDiskAmount();
            }
        }
        DecimalFormat twoDForm = new DecimalFormat("#.####");
        if (maxPer == minPer) {
            maxPer += minPer;
            minPer = 0;
        }
        if (maxDiskAmount<11){
            minDiskAmount=0;
            maxDiskAmount=11;
        }
        double intervalx = (maxDiskAmount - minDiskAmount) / 10.0;
        double intervaly = (maxPer - minPer) / 10.0;
        for (int i = 0; i < 11; i++) {
            add3DText(Double.valueOf(twoDForm.format(minDiskAmount + i * intervalx)) + "", (i * 0.1) + 0.03, -0.03, 0, 0.03, 0.03, 0.00);
            add3DText(Double.valueOf(twoDForm.format(minPer + i * intervaly)) + "", -0.03, (i * 0.1) - 0.01, 0, 0.03, 0.03, 0.00);
        }
    }
    /*private void addXTextMarks(Vector<CompressionGraphObject> allPoints) {
    double minBS = allPoints.get(0).get_bufferSize();
    double maxBS = allPoints.get(allPoints.size() - 1).get_bufferSize();
    double interval = (maxBS - minBS) / 10.0;
    for (int j = 0; j < 11; j++) {
    add3DText(minBS + j * interval + "", (j * 0.1) + 0.03, -0.03, 0, 0.03, 0.03, 0.03);
    }
    }

    private void addYTextMarks(int timeUnitsTotalnumber) {
    for (int i = 0; i < 11; i++) {
    add3DText((timeUnitsTotalnumber * i) / 10.0 + "", 0.01, (0.1 * i) - 0.01, 0, 0.035, 0.035, 0.035);
    }
    }*/

    private Vector<PointValues> normalize(Vector<CompressionGraphObject> allPoints) {        
        double minPer = allPoints.get(0).get_performance();
        double maxPer = allPoints.get(0).get_performance();
        double minDiskAmount = allPoints.get(0).get_emptyDiskAmount();
        double maxDiskAmount = allPoints.get(0).get_emptyDiskAmount();
        for (int i = 1; i < allPoints.size(); i++) {
            if (minPer > allPoints.get(i).get_performance()) {
                minPer = allPoints.get(i).get_performance();
            }
            if (maxPer < allPoints.get(i).get_performance()) {
                maxPer = allPoints.get(i).get_performance();
            }
            if (maxDiskAmount < allPoints.get(i).get_emptyDiskAmount()) {
                maxDiskAmount = allPoints.get(i).get_emptyDiskAmount();
            }
            if (minDiskAmount > allPoints.get(i).get_emptyDiskAmount()) {
                minDiskAmount = allPoints.get(i).get_emptyDiskAmount();
            }
        }
        Vector<PointValues> normalizedpoints = new Vector<PointValues>();
        CompressionGraphObject p;
        if (maxDiskAmount < 11) {
            maxDiskAmount=11;
            minDiskAmount=0;
            /*Vector<PointValues> normPoints = new Vector<PointValues>();
            normPoints.add(new PointValues(0.5f, 0.5f, 0.5f, allPoints.get(0).toString()));
            return normPoints;*/
        }
        if (maxPer==minPer){
            maxPer+=minPer;
            minPer=0;
        }
        double intervalPer = maxPer - minPer;
        double intervalDiskAmount = maxDiskAmount - minDiskAmount;
        for (int i = 0; i < allPoints.size(); i++) {
            p = allPoints.get(i);
            normalizedpoints.add(new PointValues((float) ((p.get_emptyDiskAmount() - minDiskAmount) / intervalDiskAmount), (float) ((p.get_performance() - minPer) / intervalPer), 0f, p.toString()));
        }

        return normalizedpoints;
    }

    public static void main(String[] args) {
        Vector<CompressionGraphObject> compressionResults = new Vector<CompressionGraphObject>();
        compressionResults.add(new CompressionGraphObject(1, 0.001));
        compressionResults.add(new CompressionGraphObject(2, 0.003));
        compressionResults.add(new CompressionGraphObject(5, 0.05));

        CompressionGraphData compGD = new CompressionGraphData(compressionResults);
        new ThinProvisioningGraph(compGD).setVisible(true);
    }

    @Override
    protected void setGraphLocation(Transform3D t) {
        t.setTranslation(new Vector3f(-0.5f, -0.5f, -0.5f));//graph location in window
    }
}
